<?php

namespace mpend\modules\actor\controllers;

use common\db\ActiveRecord;
use common\helpers\ArrayHelper;
use common\logics\RewardLogic;
use common\models\actor\ActorAttach;
use common\models\actor\Actor;
use common\models\actor\ActorResume;
use common\models\actor\ActorTag;
use common\models\actor\Taglib;
use common\models\user\UserAccount;
use mpend\models\UploadActorImage;
use mpend\models\User;
use mpend\modules\actor\models\ActorView;
use mpend\modules\actor\models\TagActorSearch;
use Yii;
use mpend\components\Controller;
use mpend\modules\actor\models\ActorForm;
use yii\web\UploadedFile;
use yii\data\ActiveDataProvider;

class ActorController extends Controller
{

    protected function rules()
    {
        return [
            [
                'allow' => true,
                'roles' => ['@'],
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    protected function verbs()
    {
        return [
            '*' => ['POST'],
            'award' => ['GET'],
            'user-account' => ['GET'],
            'view' => ['GET'],
            'search-actor' => ['GET']
        ];
    }

    /**
     * 修改/创建简历
     */
    public function actionCreate()
    {
        $model = new ActorForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('缺少信息');
        }
        $model->user_id = $this->uid;
        return $model->save();
    }


    /**
     * 上传简历图片（近期照片）
     */
    public function actionUpload()
    {
        $model = new ActorAttach();
        $params = Yii::$app->request->post();
        $model->setAttributes($params);
        $model->user_id = $this->uid;
        $actor = Actor::find()->active()->andWhere(['user_id' => $model->user_id])->one();
        if (!$actor) {
            return $model->dumpError('用户简历不存在');
        }
        $actor_id = $actor->id;
        $model->actor_id = $actor_id;
        if (!$model->save()) {
            return $model->getErrorMsg();
        }
        return $model->toArray();
    }

    /**
     * 获取登录用户的贡献值
     */
    public function actionUserAccount()
    {
        /* @var $model UserAccount */
        $model = UserAccount::find()->active()->andWhere(['user_id' => $this->uid])->one();
        if (!$model) {
            $model = new UserAccount();
            $model->current = 0;
        }
        return $model->current;
    }

    /**
     * 获取简历详情
     */
    public function actionDetail()
    {
        $id = Yii::$app->request->post('id');
        $query = ActorView::find()->active();
        if ($id) {
            $query->andWhere(['id' => $id]);
        } else {
            $query->andWhere(['user_id' => $this->uid]);
        }
        return $query->one();
    }

    /**
     * 获取推荐的简历列表
     */
    public function actionRecomActorList()
    {
        return Actor::find()->active()->all();
    }

    /**
     * 根据技能获取简历列表
     */
    public function actionActorList()
    {
        $params = Yii::$app->request->post();
        $model = new TagActorSearch();
        $model->load($params, '');
        return $model->search();
    }

    /**
     * 获取简历图集
     */
    public function actionActorAttach()
    {
        $params = Yii::$app->request->post();
        if ($params) {
            $attachmodel = ActorAttach::find()->active()->andWhere(['actor_id' => $params['id']])->all();
            return $attachmodel;
        } else {
            $model = Actor::find()->active()->andWhere(['user_id' => $this->uid])->one();
            if (!$model) {
                return $model->dumpError('用户简历不存在');
            }
            $attachmodel = ActorAttach::find()->active()->andWhere(['user_id' => $this->uid, 'actor_id' => $model->id])->all();
            return $attachmodel;
        }
    }

    /**
     * 删除简历图集
     */
    public function actionDelAttach()
    {
        $id = Yii::$app->request->post('id');
        $model = ActorAttach::find()->andWhere(['id' => $id])->one();
        $model->delete();
        return true;
    }

    /**
     * 获取履历列表
     */
    public function actionResumeList()
    {
        $params = Yii::$app->request->post();
        if ($params) {
            return ActorResume::find()->active()->andWhere(['actor_id' => $params['id']])->all();
        } else {
            return ActorResume::find()->active()->andWhere(['user_id' => $this->uid])->all();
        }
    }

    /**
     * 添加履历
     */
    public function actionResume()
    {
        $model = new ActorResume();
        $params = Yii::$app->request->post();
        $model->setAttributes($params);
        $model->user_id = $this->uid;
        $actor = Actor::find()->active()->andWhere(['user_id' => $model->user_id])->one();
        if (!$actor) {
            return $model->dumpError('用户简历不存在');
        }
        $actor_id = $actor->id;
        $model->actor_id = $actor_id;
        if (!$model->save()) {
            return $model->getErrorMsg();
        }
        return $model->toArray();
    }

    /**
     * 删除履历
     */
    public function actionDeleteResume()
    {

        $model = new ActorResume();
        $params = Yii::$app->request->post();
        $query = $model::find()->active()->andWhere(['id' => $params['id']])->one();
        if (!$query) {
            return $model->dumpError('履历信息不存在');
        } else {
            $transaction = Yii::$app->db->beginTransaction();
            try {
                if (!$query->delete()) {
                    throw new Exception($query->getErrorMsg());
                }
                $transaction->commit();
            } catch (Exception $e) {
                // 记录回滚（事务回滚）
                $transaction->rollBack();
                $query->flashError($e->getMessage());
            }
        }
        return [];
    }

    /**
     * 获取分享奖励
     */
    public function actionAward($uid)
    {
        $model = Actor::find()->active()->andWhere(['user_id' => $uid])->one();
        if (!$model) {
            return false;
        }
        $credit = ArrayHelper::getValue(Yii::$app->params, ['credit', 'share'], 0);
        $model->weight += $credit;
        if (!$model->save()) {
            return false;
        }
        RewardLogic::share($uid);
        return true;
    }

    /**
     * 搜索简历
     */
    public function actionSearchActor($key = null)
    {
        $k = $key ? $key : '';
        return ActorView::find()->active()->andWhere(['status' => 10])->andWhere(['like', 'name', $k])->all();
    }
}